package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liu
 * @version 1.0.0
 * @createTime 2020年10月10日
 * @Description 角色实现类
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    /**
     * 角色分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[角色分页查询service]data:{}",queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        Page<Role> page=roleDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Integer> findPermissionsBypermissionIds(Integer id) {


        return  roleDao.selectPermissionIdByRoleId(id);
    }

    @Override
    public List<Integer> findMenuBymenuIds(Integer id) {
        log.info("[查询菜单service  角色id:{}]",id);
        return  roleDao.selectMenuBymenuIds(id);
    }

    @Transactional
    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //更改role的内容   其他的编辑变关系
        roleDao.updateRole(role);

        //插入关系
        //权限
        //会存在重复的先把所有有关他的权限都删除
        roleDao.deletePermission(role.getId());
        for (Integer permissionId : permissionIds) {
       roleDao.insert(role.getId(),permissionId);
        }
        //菜单
        roleDao.deleteMenu(role.getId());
        for (Integer menuId : menuIds) {
            roleDao.insertMenu(role.getId(),menuId);
        }
    }

    @Transactional
    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.insertRole(role);
        for (Integer permissionId : permissionIds) {
            //循环插入  注意返回roleid
            roleDao.insert(role.getId(),permissionId);
        }
        for (Integer menuId : menuIds) {
            roleDao.insertMenu(role.getId(),menuId);
        }
    }

    @Override
    public List<Role> findAll() {



        return roleDao.findAll();
    }

    @Override
    public List<Integer> findRoleIdsByuserId(Integer id) {


        return roleDao.selectRoleIdsByuserId(id);
    }

    @Override
    public void delete(Integer id) {
        roleDao.deletePermission(id);
        roleDao.deleteMenu(id);
        roleDao.delete(id);
    }

    @Override
    public Role findById(Integer id) {



        return roleDao.selectById(id);
    }

}
